package cn.forward.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.forward.constants.Constants;
import cn.forward.constants.GlobalExceptionCode;
import cn.forward.exception.GlobalException;
import cn.forward.feign.AppFeignClient;
import cn.forward.mapper.CustomerMapper;
import cn.forward.param.AppLoginParam;
import cn.forward.pojo.domain.Customer;
import cn.forward.pojo.domain.CustomerSummary;
import cn.forward.pojo.domain.CustomerWallet;
import cn.forward.result.R;
import cn.forward.service.ICustomerService;
import cn.forward.service.ICustomerSummaryService;
import cn.forward.service.ICustomerWalletService;
import cn.forward.template.WechatTemplate;
import cn.forward.utils.BitStatesUtil;
import cn.forward.utils.NameUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author forward
 * @since 2025-03-24
 */
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
   private WechatTemplate wechatTemplate;

    @Autowired
    private ICustomerSummaryService summaryService;

    @Autowired
    private ICustomerWalletService customerWalletService;

    @Autowired
    private AppFeignClient appFeignClient;


    @Override
    public void register(String openIdCode) {

        String openId = wechatTemplate.openIdByCode(openIdCode);


        Customer customerObj = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openId));
        if (ObjectUtil.isNotNull(customerObj)){
            throw new GlobalException(GlobalExceptionCode.PARAM_PHONE_EXIST);
        }

         customerObj = new Customer();

        String phone = "14525418";

        saveCustomer(customerObj,openId,phone);
        Long customerId = customerObj.getId();

        saveSummaryWallet(customerId,phone);


        String password = BCrypt.hashpw("123456", BCrypt.gensalt());

        //最后调用feign
        saveLogin(openId, phone,password);


    }

    public void saveLogin(String openId, String phone,String password) {
        AppLoginParam appLoginParam = new AppLoginParam();
        appLoginParam.setUsername(NameUtil.getName());
        appLoginParam.setPassword(password);
        appLoginParam.setType(Constants.Login.TYPE_CUSTOMER);
        appLoginParam.setEnabled(true);
        appLoginParam.setAvatar(Constants.url.AVATAR);
        appLoginParam.setAdmin(false);
        appLoginParam.setNickName(NameUtil.getName());
        appLoginParam.setOpenId(openId);
        appLoginParam.setName(NameUtil.getName());
        appLoginParam.setPhone(phone);

        R<Void> R = appFeignClient.sava(appLoginParam);
        if (! R.isSuccess()){
            throw new GlobalException(GlobalExceptionCode.WECHAT_REGISTER_ERROR);
        }
    }

    public void saveSummaryWallet(Long customerId, String phone) {

        CustomerWallet customerWallet = new CustomerWallet();

        customerWallet.setId(customerId);
        customerWallet.setAmount(new BigDecimal("0"));
        customerWallet.setPassword(phone);
        customerWallet.setDeleted(false);
        customerWallet.setVersion(0);
        customerWallet.setCreateTime(new Date());
        customerWalletService.save(customerWallet);


        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(customerId);
        customerSummary.setTotalOrders(0);
        customerSummary.setTotalFine(new BigDecimal("0"));
        customerSummary.setVoucherNumbers(0);
        customerSummary.setCancelOrders(0);

        summaryService.save(customerSummary);
    }

    public void saveCustomer(Customer customer, String openId,String phone) {
        customer.setSex(Constants.SEX_MAN);
        customer.setPhone(phone);
        customer.setEmail("qq.com");
        customer.setCreateTime(new Date());
        customer.setVersion(0);
        customer.setDeleted(false);
        customer.setOpenId(openId);
        customer.setBitState(BitStatesUtil.OP_PHONE);
        customer.setName(NameUtil.getName());
        customer.setLevel(Constants.Level.LEVEL_BRONZE);

        super.save(customer);

    }
}
